package com.jk.tweetsnear.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

import twitter4j.TwitterException;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.widget.Toast;

import com.jk.tweetsnear.application.App;
import com.jk.tweetsnear.gps.GPSTracker;

public class Utils
{
	public static boolean DEBUG = false;
	public static final String FILLTER = "TweetsNear";
	public static final String TAG_OPTION = " : ";
	public static final String TRACKER_ID = "UA-43130540-1";

	public static String compateCreatedAt(twitter4j.Status status)
	{
		long diff = 0;
		long diffDays = 0;

		String currentDate = null;
		String compareDate = null;
		String createAt = null;

		try
		{
			Date date1 = status.getCreatedAt();
			Date date2 = new Date();

			SimpleDateFormat sdf1 = new java.text.SimpleDateFormat("yyyyMMdd");
			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
			SimpleDateFormat sdf3 = new SimpleDateFormat("yyyyMMdd");

			compareDate = sdf1.format(date1);
			currentDate = sdf2.format(date2).toString();

			Date beginDate = sdf3.parse(compareDate);
			Date endDate = sdf3.parse(currentDate);

			diff = endDate.getTime() - beginDate.getTime();
			diffDays = diff / (24 * 60 * 60 * 1000);
		} catch (Exception e)
		{
			e.printStackTrace();
		}

		if (diffDays == 0)
			createAt = "today";
		else
			createAt = diffDays + "day";

		return createAt;
	}

	public static void getCurrentLocation(Activity act)
	{
		GPSTracker gps = new GPSTracker(act);

		if (gps != null)
		{

			if (gps.canGetLocation())
			{
				App.currentLat = gps.getLatitude();
				App.currentLon = gps.getLongitude();

				//Toast.makeText(act, "Your Location is - \nLat: " + App.currentLat + "\nLong: " + App.currentLon, Toast.LENGTH_LONG).show();
			} else
			{
				// gps 설정이 되어있지 않음 , 연결 하는 다이얼로그.
				gps.showSettingsAlert(act);
			}
		}
	}
	
	public static void setExit(Activity act) {
		int sdkVersion = Integer.parseInt(Build.VERSION.SDK);
		if (sdkVersion < 8) {
			ActivityManager am = (ActivityManager) act.getSystemService(Context.ACTIVITY_SERVICE);
			am.restartPackage(act.getPackageName());
		} else {
			act.moveTaskToBack(true);
			act.finish();
			android.os.Process.killProcess(android.os.Process.myPid());
		}
		return;
	}	
	
	public static void showFollowingAlert(final Activity act, final String screenName)
	{
		AlertDialog.Builder alertDialog = new AlertDialog.Builder(act);

		alertDialog.setCancelable(false);
		alertDialog.setMessage("@"+screenName + ", Following?");
		alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int which)
			{
				if(App.twitter != null)
				{
					try
					{
						App.twitter.createFriendship(screenName);
						Toast.makeText(act, screenName + ", Following", Toast.LENGTH_SHORT).show();
					} catch (TwitterException e)
					{
						dialog.cancel();
						Toast.makeText(act, screenName + ", Following fail", Toast.LENGTH_SHORT).show();
						e.printStackTrace();
					}
				}
			}
		});

		alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int which)
			{
				dialog.cancel();
			}
		});

		alertDialog.show();
	}	
	
	public static void showAlreadyFollowingAlert(final Activity act, final String screenName)
	{
		AlertDialog.Builder alertDialog = new AlertDialog.Builder(act);

		alertDialog.setCancelable(false);
		alertDialog.setMessage("@"+screenName + " Already Following");
		alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int which)
			{
				dialog.dismiss();
			}
		});
		alertDialog.setNegativeButton("UnFollowing", new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int which)
			{
				if(App.twitter != null)
				{
					try
					{
						App.twitter.createFriendship(screenName);
						Toast.makeText(act, screenName + ", UnFollowing", Toast.LENGTH_SHORT).show();
					} catch (TwitterException e)
					{
						dialog.cancel();
						Toast.makeText(act, screenName + ", UnFollowing fail", Toast.LENGTH_SHORT).show();
						e.printStackTrace();
					}
				}
			}
		});			


		alertDialog.show();
	}
	
	public static void showAlert(final Activity act, final String message)
	{
		AlertDialog.Builder alertDialog = new AlertDialog.Builder(act);

		alertDialog.setCancelable(true);
		alertDialog.setMessage(message);
		alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener()
		{
			public void onClick(DialogInterface dialog, int which)
			{
				
				setRecommend(act, true);
				
				Intent intent = new Intent();
				intent.setData(Uri.parse("market://details?id=com.jk.tweetsnear")); 
				act.startActivity(intent);
			}
		});
		
		alertDialog.show();
	}
	
	public static boolean isRecommend(Activity act)
	{
		SharedPreferences prefs = act.getSharedPreferences("prefRecommend", Activity.MODE_PRIVATE);
		boolean recommend = prefs.getBoolean("pref_key_recommend", false);
		return recommend;
	}
	
	public static void setRecommend(Activity act, boolean recommend)
	{
		SharedPreferences prefs = act.getSharedPreferences("prefRecommend", Activity.MODE_PRIVATE);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putBoolean("pref_key_recommend", recommend);
		editor.commit();
	}	

}
